//
//  filterViewController.m
//  one_mile_01
//
//  Created by 王进帅 on 15/11/2.
//  Copyright © 2015年 王雅蓉. All rights reserved.
//

#import "filterViewController.h"

@interface filterViewController ()
{
    NSString *filter_contryKey;
    NSString *filter_professionKey;
    NSString *filter_majorKey;
    NSString *filter_hobbyKey;
}

@end

@implementation filterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACKGROUND_ASSISTANT_COLOR;
    
    UIView *navigationV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, NAVIGATIONHEIGHT)];
    navigationV.backgroundColor = BACKGROUND_MAIN_COLOR;
    [self.view addSubview:navigationV];
    
    //    返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = POP_FRAME;
    [button setBackgroundImage:[UIImage imageNamed:@"pop_light@2x.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.navigationController.navigationBar.hidden = YES;
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:NAVIGATION_TITLE_FRAME];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = NAVIGATION_TITLE_FONT;
    titleLabel.text = @"筛选";
    [self.view addSubview:titleLabel];

    // Do any additional setup after loading the view.
    UILabel * countryL = [[UILabel alloc]initWithFrame:CGRectMake(10 * FITRATE, (69 + 15) * FITRATE, 100 * FITRATE, 20 * FITRATE)];
    countryL.text = @"国家";
    [self.view addSubview:countryL];
    
    for (int i = 0; i < 5; i++) {
        UIButton *countryButton = [UIButton buttonWithType:UIButtonTypeCustom];
        countryButton.tag = 13300 + i;
        
        for (int j = 0; j <= (i / 4); j++) {
            countryButton.layer.masksToBounds = YES;
            countryButton.layer.cornerRadius = 13.0 * FITRATE;
            [countryButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            countryButton.frame = CGRectMake(15 * FITRATE + (i % 4) * (79 + 9) * FITRATE, countryL.frame.origin.y + countryL.frame.size.height + 13 * FITRATE + j * (25 + 10) * FITRATE, 79 * FITRATE, 25 * FITRATE);
            countryButton.titleLabel.font = [UIFont systemFontOfSize:15 * FITRATE];
            [countryButton addTarget:self action:@selector(contryButton:) forControlEvents:UIControlEventTouchUpInside];
            countryButton.backgroundColor = RGBACOLOR(229, 227, 238, 1);
            
            if (countryButton.tag == 13300) {
                
                [countryButton setTitle:@"加拿大" forState:UIControlStateNormal];
            }else if (countryButton.tag == 13301) {
                
                [countryButton setTitle:@"新加坡" forState:UIControlStateNormal];
            }else if (countryButton.tag == 13302) {
                
                [countryButton setTitle:@"美国" forState:UIControlStateNormal];
            }else if (countryButton.tag == 13303) {
                
                [countryButton setTitle:@"英国" forState:UIControlStateNormal];
            }else if (countryButton.tag == 13304) {
                
                [countryButton setTitle:@"法国" forState:UIControlStateNormal];
            }
            
            
            [self.view addSubview:countryButton];
        }
    }
    UILabel *lineOne = [[UILabel alloc]initWithFrame:CGRectMake(countryL.frame.origin.x, 195 * FITRATE, WIDTH - 30 * FITRATE, 1 * FITRATE)];
    lineOne.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineOne];
    
    UILabel * xueliL = [[UILabel alloc]initWithFrame:CGRectMake(lineOne.frame.origin.x, lineOne.frame.origin.y + 1 * FITRATE + 15 * FITRATE, 100 * FITRATE, 20 * FITRATE)];
    xueliL.text = @"学历";
    [self.view addSubview:xueliL];
    
    for (int i = 0; i < 3; i++) {
        UIButton *xueliButton = [UIButton buttonWithType:UIButtonTypeCustom];
        xueliButton.tag = 13400 + i;
        
        for (int j = 0; j <= (i / 4); j++) {
            
            [xueliButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            xueliButton.backgroundColor = RGBACOLOR(229, 227, 238, 1);
            xueliButton.layer.masksToBounds = YES;
            xueliButton.layer.cornerRadius = 13 * FITRATE;
            xueliButton.frame = CGRectMake(15 * FITRATE + (i % 4) * (79 + 9) * FITRATE, xueliL.frame.origin.y + xueliL.frame.size.height + 13 * FITRATE + j * (25 + 10) * FITRATE, 79 * FITRATE, 25 * FITRATE);
            xueliButton.titleLabel.font = [UIFont systemFontOfSize:15];
            [xueliButton addTarget:self action:@selector(xueliButton:) forControlEvents:UIControlEventTouchUpInside];
            
            if (xueliButton.tag == 13400) {
                
                [xueliButton setTitle:@"研究生" forState:UIControlStateNormal];
            }else if (xueliButton.tag == 13401) {
                
                [xueliButton setTitle:@"博士" forState:UIControlStateNormal];
            }else if (xueliButton.tag == 13402) {
                
                [xueliButton setTitle:@"本科" forState:UIControlStateNormal];
            }
            [self.view addSubview:xueliButton];
        }
    }
    
    UILabel *lineTwo = [[UILabel alloc]initWithFrame:CGRectMake(countryL.frame.origin.x, 284 * FITRATE, WIDTH - 30 * FITRATE, 1 * FITRATE)];
    lineTwo.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineTwo];
    
    UILabel * zhuanyeL = [[UILabel alloc]initWithFrame:CGRectMake(lineTwo.frame.origin.x, lineTwo.frame.origin.y + 1 * FITRATE + 15 * FITRATE, 100 * FITRATE, 20 * FITRATE)];
    zhuanyeL.text = @"专业";
    [self.view addSubview:zhuanyeL];
    
    for (int i = 0; i < 9; i++) {
        
        UIButton *zhuanyeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        zhuanyeButton.tag = 13500 + i;
        
        for (int j = 0; j <= (i / 4); j++) {
            
            zhuanyeButton.backgroundColor = RGBACOLOR(229, 227, 238, 1);
            zhuanyeButton.layer.masksToBounds = YES;
            zhuanyeButton.layer.cornerRadius = 13 * FITRATE;
            zhuanyeButton.frame = CGRectMake(15 * FITRATE + (i % 4) * (79 + 9) * FITRATE, zhuanyeL.frame.origin.y + zhuanyeL.frame.size.height + 13 * FITRATE + j * (25 + 10) * FITRATE, 79 * FITRATE, 25 * FITRATE);
            [zhuanyeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            zhuanyeButton.titleLabel.font = [UIFont systemFontOfSize:15 * FITRATE];
            [zhuanyeButton addTarget:self action:@selector(zhuanyeButton:) forControlEvents:UIControlEventTouchUpInside];
            
            if (zhuanyeButton.tag == 13500) {
                
                [zhuanyeButton setTitle:@"土木工程" forState:UIControlStateNormal];
            }else if (zhuanyeButton.tag == 13501) {
                
                [zhuanyeButton setTitle:@"电子工程" forState:UIControlStateNormal];
            }else if (zhuanyeButton.tag == 13502) {
                
                [zhuanyeButton setTitle:@"金融工程" forState:UIControlStateNormal];
            }else if (zhuanyeButton.tag == 13503) {
                
                [zhuanyeButton setTitle:@"材料工程" forState:UIControlStateNormal];
            }else if (zhuanyeButton.tag == 13504) {
                
                [zhuanyeButton setTitle:@"计算机" forState:UIControlStateNormal];
            }else if (zhuanyeButton.tag == 13505) {
                
                [zhuanyeButton setTitle:@"物理" forState:UIControlStateNormal];
            }else if (zhuanyeButton.tag == 13506) {
                
                [zhuanyeButton setTitle:@"金融" forState:UIControlStateNormal];
            }else if (zhuanyeButton.tag == 13507) {
                
                [zhuanyeButton setTitle:@"心理" forState:UIControlStateNormal];
            }else if (zhuanyeButton.tag == 13508) {
                
                [zhuanyeButton setTitle:@"生物" forState:UIControlStateNormal];
            }
            
            [self.view addSubview:zhuanyeButton];
        }
    }
    
    UILabel *lineThree = [[UILabel alloc]initWithFrame:CGRectMake(zhuanyeL.frame.origin.x, 442 * FITRATE, WIDTH - 30 * FITRATE, 1 * FITRATE)];
    lineThree.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lineThree];
    
    UILabel * hobbyL = [[UILabel alloc]initWithFrame:CGRectMake(lineThree.frame.origin.x, lineThree.frame.origin.y + 1 * FITRATE + 15 * FITRATE, 100 * FITRATE, 20 * FITRATE)];
    hobbyL.text = @"爱好";
    [self.view addSubview:hobbyL];
    
    for (int i = 0; i < 5; i++) {
        UIButton *hobbyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        hobbyButton.tag = 13600 + i;
        
        for (int j = 0; j <= (i / 4); j++) {
            
            hobbyButton.backgroundColor = RGBACOLOR(229, 227, 238, 1);
            
            
            hobbyButton.layer.masksToBounds = YES;
            hobbyButton.layer.cornerRadius = 13 * FITRATE;
            hobbyButton.frame = CGRectMake(15 * FITRATE + (i % 4) * (79 + 9) * FITRATE, hobbyL.frame.origin.y + hobbyL.frame.size.height + 13 * FITRATE + j * (25 + 10) * FITRATE, 79 * FITRATE, 25 * FITRATE);
            hobbyButton.titleLabel.font = [UIFont systemFontOfSize:15 * FITRATE];
            [hobbyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [hobbyButton addTarget:self action:@selector(hobbyButton:) forControlEvents:UIControlEventTouchUpInside];
            
            if (hobbyButton.tag == 13600) {
                
                [hobbyButton setTitle:@"骑马" forState:UIControlStateNormal];
            }else if (hobbyButton.tag == 13601) {
                
                [hobbyButton setTitle:@"赛车" forState:UIControlStateNormal];
            }else if (hobbyButton.tag == 13602) {
                
                [hobbyButton setTitle:@"旅游" forState:UIControlStateNormal];
            }else if (hobbyButton.tag == 13603) {
                
                [hobbyButton setTitle:@"摄影" forState:UIControlStateNormal];
            }else if (hobbyButton.tag == 13604) {
                
                [hobbyButton setTitle:@"健身" forState:UIControlStateNormal];
            }
            [self.view addSubview:hobbyButton];
        }
    }
    
    UIButton  *reSettingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    reSettingButton.frame = CGRectMake(WIDTH / 2.0 - 49 * FITRATE, 565 * FITRATE, 98 * FITRATE, 32 * FITRATE);
    reSettingButton.layer.borderWidth = 1.0 * FITRATE;
    reSettingButton.layer.borderColor = BACKGROUND_MAIN_COLOR.CGColor;
    [reSettingButton setTitle:@"重置" forState:UIControlStateNormal];
    [reSettingButton setTitleColor:BACKGROUND_MAIN_COLOR forState:UIControlStateNormal];
    [reSettingButton addTarget:self action:@selector(reSettingButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:reSettingButton];
    
    
    UIButton *confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(0, HEIGHT - 50 * FITRATE, WIDTH, 50 * FITRATE)];
    confirmButton.backgroundColor = BACKGROUND_MAIN_COLOR;
    [confirmButton setTitle:@"确认" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:confirmButton];
    
    [confirmButton addTarget:self action:@selector(querryFilterAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // Do any additional setup after loading the view, typically from a nib.
}

//国家
-(void)contryButton:(UIButton *)button{
    
    for (int i = 0; i < 5; i++) {
        UIButton *tmpBT = (UIButton *)[self.view viewWithTag:13300 + i];
        if (button.tag == 13300 + i) {
            [tmpBT setBackgroundColor:BACKGROUND_MAIN_COLOR];
            [tmpBT setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        } else {
            [tmpBT setBackgroundColor:RGBACOLOR(229, 227, 238, 1)];
            [tmpBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    filter_contryKey = button.titleLabel.text;
}

//学历
-(void)xueliButton:(UIButton *)button{
    
    for (int i = 0; i < 3; i++) {
        UIButton *tmpBT = (UIButton *)[self.view viewWithTag:13400 + i];
        if (button.tag == 13400 + i) {
            [tmpBT setBackgroundColor:BACKGROUND_MAIN_COLOR];
            [tmpBT setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        } else {
            [tmpBT setBackgroundColor:RGBACOLOR(229, 227, 238, 1)];
            [tmpBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    filter_professionKey = button.titleLabel.text;
}

//专业
-(void)zhuanyeButton:(UIButton *)button{
    
    for (int i = 0; i < 9; i++) {
        UIButton *tmpBT = (UIButton *)[self.view viewWithTag:13500 + i];
        if (button.tag == 13500 + i) {
            [tmpBT setBackgroundColor:BACKGROUND_MAIN_COLOR];
            [tmpBT setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        } else {
            [tmpBT setBackgroundColor:RGBACOLOR(229, 227, 238, 1)];
            [tmpBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    filter_majorKey = button.titleLabel.text;
}

//爱好
-(void)hobbyButton:(UIButton *)button{
    
    for (int i = 0; i < 5; i++) {
        UIButton *tmpBT = (UIButton *)[self.view viewWithTag:13600 + i];
        if (button.tag == 13600 + i) {
            [tmpBT setBackgroundColor:BACKGROUND_MAIN_COLOR];
            [tmpBT setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        } else {
            [tmpBT setBackgroundColor:RGBACOLOR(229, 227, 238, 1)];
            [tmpBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    filter_hobbyKey = button.titleLabel.text;
}

//重置
-(void)reSettingButton:(UIButton *)button{
    
    for (int i = 0; i < 5; i++) {
        
        UIButton *tmpBT = (UIButton *)[self.view viewWithTag:13600 + i];
        tmpBT.backgroundColor = RGBACOLOR(229, 227, 238, 1);
        [tmpBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    for (int i = 0; i < 5; i++) {
        
        UIButton *tmpBT = (UIButton *)[self.view viewWithTag:13300 + i];
        [tmpBT setBackgroundColor:RGBACOLOR(229, 227, 238, 1)];
        [tmpBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    for (int i = 0; i < 9; i++) {
        
        UIButton *tmpBT = (UIButton *)[self.view viewWithTag:13500 + i];
        [tmpBT setBackgroundColor:RGBACOLOR(229, 227, 238, 1)];
        [tmpBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    for (int i = 0; i < 3; i++) {
        
        UIButton *tmpBT = (UIButton *)[self.view viewWithTag:13400 + i];
        [tmpBT setBackgroundColor:RGBACOLOR(229, 227, 238, 1)];
        [tmpBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    filter_contryKey = @"";
    filter_professionKey = @"";
    filter_majorKey = @"";
    filter_hobbyKey = @"";
}

//navigationButton
-(void)backbutton:(UIButton *)button{
    
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2] animated:YES];
}

//确定
-(void)querryFilterAction:(UIButton *)button
{
    NSString *keywords;
    if (filter_contryKey.length != 0) {
        keywords = filter_contryKey;
        if (filter_professionKey.length != 0) {
            keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_professionKey]];
            if (filter_majorKey.length != 0) {
                keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_majorKey]];
                if (filter_hobbyKey.length != 0) {
                    keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_hobbyKey]];
                }
            } else {
                if (filter_hobbyKey.length != 0) {
                    keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_hobbyKey]];
                }
            }
        } else {
            if (filter_majorKey.length != 0) {
                keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_majorKey]];
                if (filter_hobbyKey.length != 0) {
                    keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_hobbyKey]];
                }
            } else {
                if (filter_hobbyKey.length != 0) {
                    keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_hobbyKey]];
                }
            }
        }
    } else {
        if (filter_professionKey.length != 0) {
            keywords = [NSString stringWithFormat:@"%@", filter_professionKey];
            if (filter_majorKey.length != 0) {
                keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_majorKey]];
                if (filter_hobbyKey.length != 0) {
                    keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_hobbyKey]];
                }
            } else {
                if (filter_hobbyKey.length != 0) {
                    keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_hobbyKey]];
                }
            }
        } else {
            if (filter_majorKey.length != 0) {
                keywords = [NSString stringWithFormat:@"%@", filter_majorKey];
                if (filter_hobbyKey.length != 0) {
                    keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_hobbyKey]];
                }
            } else {
                if (filter_hobbyKey.length != 0) {
                    keywords = [keywords stringByAppendingString:[NSString stringWithFormat:@",%@", filter_hobbyKey]];
                }
            }
        }
    }
    
    [self.myDelegate sendFilterKeywords:keywords];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
